package com.ziyueyuwen.xiniao.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ziyueyuwen.xiniao.bean.product.ZyCouponDO;
import com.ziyueyuwen.xiniao.dao.ZyCouponMapper;
import com.ziyueyuwen.xiniao.enums.CouponStatusEnum;
import com.ziyueyuwen.xiniao.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.ziyueyuwen.xiniao.util.ConstUtil.KEY_INFO_LIST;
import static com.ziyueyuwen.xiniao.util.ConstUtil.KEY_TOTAL;

/**
 * @author jun 2019/2/23
 */
@Service
public class CouponServiceImpl implements CouponService {

    @Autowired
    private ZyCouponMapper zyCouponMapper;

    @Override
    public Boolean add(ZyCouponDO zyCouponDO) {
        return zyCouponMapper.add(zyCouponDO);
    }

    @Override
    public Boolean delete(Integer id) {
        return zyCouponMapper.delete(id);
    }

    @Override
    public JSONObject listByManager(Integer pageNum, Integer pageSize, CouponStatusEnum couponStatus, String queryData) {
        JSONObject retObj = new JSONObject();
        Page page = PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc ");
        List<ZyCouponDO> retList = zyCouponMapper.listByManager(couponStatus, queryData);
        retObj.put(KEY_TOTAL, page.getTotal());
        retObj.put(KEY_INFO_LIST, retList);
        return retObj;
    }

    @Override
    public List<ZyCouponDO> listByUser(String userAccount) {
        return zyCouponMapper.listByUser(userAccount);
    }

    @Override
    public ZyCouponDO getByUser(String userAccount) {
        return zyCouponMapper.getByUser(userAccount);
    }

    @Override
    public Boolean usedCoupon(Integer id) {
        return zyCouponMapper.usedCoupon(id);
    }
}
